package com.ruoyi.web.controller.bus.appletsController;

import com.ruoyi.bus.domain.WitRoute;
import com.ruoyi.bus.domain.WitRouteSite;
import com.ruoyi.bus.service.IApiService;
import com.ruoyi.bus.service.IWitRouteService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.service.ISysDeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/common")
public class CommonApiController extends BaseController {

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private IWitRouteService routeService;
    
    @Autowired
    private IApiService apiService;
    
    /**
     * @Description 获取路线 司机 班车数量接口
     * @Author caiyanshan
     * @Date 2021/4/26 10:49
     * @Param []
     * @Return com.ruoyi.common.core.domain.AjaxResult
     */
    @GetMapping("getDataCount")
    public AjaxResult getDataCount(){
        return apiService.getDataCount();
    }
    
    @GetMapping("getDictParam")
    public AjaxResult getDictParam(@RequestParam String dictType){
        return apiService.getDictParam(dictType);
    }
    
    @GetMapping("listBanner")
    public AjaxResult listBanner(){
        return apiService.listBanner();
    }
    
    /**
     * @Description 获取系统内置参数
     * @Author caiyanshan
     * @Date 2021/4/27 9:46
     * @Param [key]
     * @Return com.ruoyi.common.core.domain.AjaxResult
     */
    @GetMapping("getSysParam")
    public AjaxResult getSysParam(@RequestParam String key){
        return apiService.getSysParam(key);
    }

    /**
     * 用户openId登录(无密码,类似静默授权)
     * @param openId
     * @return
     */
    @GetMapping("openIdLogin")
    public AjaxResult openIdLogin(@RequestParam String openId){
        return apiService.openIdLogin(openId);
    }

    @GetMapping("getNotice")
    public AjaxResult getNotice(){
        return AjaxResult.success(apiService.getNoticeByID());
    }

    /**
     * 更新用户名
     * @param userName
     * @return
     */
    @GetMapping("updateUserName")
    public AjaxResult updateUserName(@RequestParam String userName){
        return apiService.updateUserName(userName);
    }

    @GetMapping("updateUserDeptId")
    public AjaxResult updateUserDeptId(@RequestParam Long deptId){
        return apiService.updateUserDept(deptId);
    }

    @GetMapping("updatePassWord")
    public AjaxResult updatePassWord(@RequestParam String passWord){
        return apiService.updatePassWord(passWord);
    }

    //路线列表,按照路线名字查找
    @GetMapping("listRoute")
    public AjaxResult listRouteByRouteName(String routeName){
        WitRoute route = new WitRoute();
        route.setRouteName(routeName);
        route.setRouteStatus(0);
        return AjaxResult.success(routeService.selectWitRouteList(route));
    }
    
    //路线列表,按照站点名称查找
    @GetMapping("listRouteBySiteName")
    public AjaxResult listRouteBySiteName(String siteName){
        WitRoute route = new WitRoute();
        route.setSiteName(siteName);
        route.setRouteStatus(0);
        return AjaxResult.success(routeService.selectWitRouteList(route));
    }

    @GetMapping("listRouteBySiteOrRouteName")
    public AjaxResult listRouteBySiteOrRouteName(String paramName){
        WitRoute route = new WitRoute();
        route.setParamName(paramName);
        route.setRouteStatus(0);
        return AjaxResult.success(routeService.selectWitRouteList(route));
    }

    //线路详情
    @GetMapping("getRouteDetail")
    public AjaxResult getRouteDetail(@RequestParam Long routeId){
        WitRoute witRoute = routeService.selectWitRouteById(routeId);
        if(witRoute == null){
            return AjaxResult.success();
        }
        if(witRoute.getWitRouteSiteList() == null){
            return AjaxResult.success();
        }
        Map<String, Object> label = null;
        for(WitRouteSite witRouteSite : witRoute.getWitRouteSiteList()) {
            label = new HashMap<>();
            label.put("content", witRouteSite.getSiteName());
            label.put("bgColor", "#ffffff");
            label.put("padding", 10);
            label.put("textAlign", "center");
            label.put("borderRadius", 10);
            witRouteSite.setLabel(label);
            witRouteSite.setId(witRouteSite.getSiteId());
            witRouteSite.setNum("12333");
        }
        return AjaxResult.success(witRoute);
    }


    
    /**
     * 机构内容，获取祖籍列表，数组长度为2是2级单位，3是3级单位，4是4级单位以此类推
     */
    @GetMapping("listSecondDept")
    public AjaxResult listSecondDept(){
        return this.listDeptByLevel(2, 100l);
    }

    @GetMapping("listThirdDept")
    public AjaxResult listThirdDept(@RequestParam Long deptId){
        return this.listDeptByLevel(3, deptId);
    }

    @GetMapping("listFourthDept")
    public AjaxResult listFourthDept(@RequestParam Long deptId){
        return this.listDeptByLevel(4, deptId);
    }

    /**
     * 按照等级获取机构数据
     * @return
     */
    private AjaxResult listDeptByLevel(Integer level, Long deptId){
        List<SysDept> sysDeptList = listAllDept();
        List<SysDept> deptListTemp = new ArrayList<SysDept>();
        for(SysDept item : sysDeptList) {//获取要处理的数据
            if(item.getParentId().longValue() == deptId.longValue()){
                deptListTemp.add(item);
            }
        }
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        Map<String,Object> resultMap = null;
        for(SysDept sysDept : deptListTemp){
            String ancestors = sysDept.getAncestors();
            if(ancestors.split(",").length == level){//单位等级
                resultMap = new HashMap<String, Object>();
                resultMap.put("deptName", sysDept.getDeptName());
                resultMap.put("deptId", sysDept.getDeptId());
                list.add(resultMap);
            }
        }
        return AjaxResult.success(list);
    }

    /**
     * 获取所有机构对象
     * @return
     */
    private List<SysDept> listAllDept(){
        SysDept dept = new SysDept();
        dept.setStatus("0");
        return sysDeptService.selectDeptList(dept);
    }

}
